package org.yesin.auth.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yesin.auth.client.UserClient;
import org.yesin.auth.config.JwtProperties;
import org.yesin.auth.entity.UserInfo;
import org.yesin.auth.utils.JwtUtils;
import org.yesin.user.pojo.User;

@Service
public class AuthService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties pro;

    public String accredit(String username, String password) {
        // 1. 远程调用queryUser方法，来验证用户名和密码，获取user
        User user = this.userClient.query(username, password);
        // 2. 判断user是否为空
        if (user == null){
            return null;
        }
        try {
            UserInfo userInfo = new UserInfo(user.getId(), user.getUsername());
            // 3. 通过jwt工具类生成token
            return JwtUtils.generateToken(userInfo, pro.getPrivateKey(), pro.getExpire());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
